﻿#region Usings

using System;
using System.Xml.XPath;
using Yandex.Market.Api.YmlSerialization;

#endregion

namespace Yandex.Market.Api.Model
{
    /// <summary>
    ///   Описание vendor.model
    /// </summary>
    [YmlSerializeAs("offer")]
    [Serializable]
    public class OfferVendorModel : OfferBase
    {
        /// <summary>
        /// 
        /// </summary>
        public OfferVendorModel()
        {}

        internal OfferVendorModel(IOffer offer) : base(offer)
        {}

        /// <summary>
        /// </summary>
        [YmlAttributeForClass]
        [YmlSerializeAs("type")]
        [YmlAttributeOrder(10)]
        public YmlOfferType OfferType
        {
            get { return YmlOfferType.VendorModel; }
        }

        /// <summary>
        ///   Группа товаров \ категория
        /// </summary>
        [YmlSerializeAs("typePrefix")]
        [YmlElementOrder(200)]
        public string TypePrefix { set; get; }

        /// <summary>
        ///   Производитель. Обязательный элемент.
        /// </summary>
        [YmlSerializeAs("vendor")]
        [YmlElementOrder(210)]
        public string Vendor { set; get; }

        /// <summary>
        ///   Код товара (указывается код производителя). Необязательный элемент.
        /// </summary>
        [YmlSerializeAs("vendorCode")]
        [YmlElementOrder(220)]
        public string VendorCode { set; get; }

        /// <summary>
        ///   Модель<br />
        ///   Обязательный элемент.
        /// </summary>
        [YmlSerializeAs("model")]
        [YmlElementOrder(230)]
        public string Model { set; get; }

        /// <summary>
        /// </summary>
        [YmlSerializeAs("provider")]
        [YmlElementOrder(240)]
        public string Provider { get; set; }

        /// <summary>
        /// </summary>
        [YmlSerializeAs("tarifplan")]
        [YmlElementOrder(250)]
        public string TarifPlan { get; set; }

        internal static OfferVendorModel CreateOfferVendorModelFromXml(XPathNavigator nav, IOffer dummyOffer)
        {
            OfferVendorModel offer = new OfferVendorModel(dummyOffer);

            XPathNavigator node = nav.SelectSingleNode("typePrefix");
            if(node != null)
                offer.TypePrefix = node.Value;

            node = nav.SelectSingleNode("vendor");
            if(node != null)
                offer.Vendor = node.Value;

            node = nav.SelectSingleNode("vendorCode");
            if(node != null)
                offer.VendorCode = node.Value;

            node = nav.SelectSingleNode("model");
            if(node != null)
                offer.Model = node.Value;

            node = nav.SelectSingleNode("provider");
            if(node != null)
                offer.Provider = node.Value;

            node = nav.SelectSingleNode("tarifplan");
            if(node != null)
                offer.TarifPlan = node.Value;

            return offer;
        }
    }
}